package com.zhl.collectionsTest3;

import java.util.*;

public class Room {
    //必须有一副牌
    private List<Card> allCards = new ArrayList<>();

    public Room(){
        //1.做出54张牌，存放入集合allCards中
        //a.点数：个数确定了，类型确定
        String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K","A", "2"};
        //b.花色：黑桃、红桃、方块、梅花
        String[] colors = {"♠", "♥", "♦", "♣"};
        int size = 0;  //表示每张牌的大小
        //c.遍历点数，再遍历花色，生成一副牌
        for (String number : numbers) {
            //遍历点数
            size++;
            for (String color : colors) {
                //遍历花色  得到一张牌
                Card c = new Card(number, color, size);
                allCards.add(c);
            }
        }
        //单独加入大小王
        Card c1 = new Card("小王", "", ++size);
        Card c2 = new Card("大王", "", ++size);
        Collections.addAll(allCards, c1, c2);
        System.out.println("一副牌已经准备好！"+allCards);


    }

    public void startGame() {
        //1.洗牌
        Collections.shuffle(allCards);
        System.out.println("牌已经洗好了！"+allCards);
        //2.发牌  要定义三个玩家 使用ArrayList 集合有重复、有顺序、有索引
        List<Card> players1 = new ArrayList<>();
        List<Card> players2 = new ArrayList<>();
        List<Card> players3 = new ArrayList<>();
        //3.发牌   依次发出51张牌  剩余三张为大小王
        for(int i=0;i<51;i++){
            if(i%3==0){
                players1.add(allCards.get(i));
            }else if(i%3==1){
                players2.add(allCards.get(i));
            }else if(i%3==2){
                players3.add(allCards.get(i));
            }
        }
        //4.对玩家的牌进行排序
        sortCards(players1);
        sortCards(players2);
        sortCards(players3);

        //5.看牌
        System.out.println("玩家1："+players1);
        System.out.println("玩家2："+players2);
        System.out.println("玩家3："+players3);
        List<Card> lastthreeCards  = allCards.subList(allCards.size()-3, allCards.size());
        System.out.println("最后三张底牌："+lastthreeCards);
            }

    private void sortCards(List<Card> cards) {
        Collections.sort(cards,new Comparator<Card>(){
           @Override
           public int compare(Card c1,Card c2){
               return c1.getSize()-c2.getSize();//按照大小排序 升序
           }
        });
    }
}


